import { delCategory, getCategory, putCategory } from '@/services';
import { ICategoryItem } from '@/types';
import { Effect, Reducer } from 'umi';
export interface CategoryModelState {
  categoryDetail: Partial<ICategoryItem>;
  categoryAdd: Partial<ICategoryItem>;
  categoryDel: Partial<ICategoryItem>;
}
export interface CategoryModelType {
  namespace: 'category';
  state: CategoryModelState;
  effects: {
    getCategory: Effect;
    putCategory: Effect;
    delCategory: Effect;
  };
  reducers: {
    save: Reducer<CategoryModelState>;
  };
}
const EditorModel: CategoryModelType = {
  namespace: 'category',
  state: {
    categoryDetail: {},
    categoryAdd: {},
    categoryDel: {},
  },
  effects: {
    *getCategory({ payload }, { call, put }) {
      let result = yield call(
        getCategory,
        payload.id,
        payload.label,
        payload.value,
      );
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            categoryDetail: result.data,
          },
        });
      }
    },
    *putCategory({ payload }, { call, put }) {
      let result = yield call(putCategory, payload.label, payload.value);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            categoryAdd: result.data,
          },
        });
      }
    },
    *delCategory({ payload }, { call, put }) {
      let result = yield call(delCategory, payload.id);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            categoryDel: result.data,
          },
        });
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default EditorModel;
